var msg = function(title, msg, icon){
    Ext.Msg.show({
        title: title,
        msg: msg,
        minWidth: 200,
        modal: true,
        icon: (icon != null) ? icon : Ext.Msg.INFO,
        buttons: Ext.Msg.OK
    });
};
var warning = function(title, message, icon){
    msg(title, message, Ext.MessageBox.WARNING);
};

var thousandSeparator = '.';
var decimalSeparator = ',';
var decimals = 2;

function forceNumber(event){
    if ((event.keyCode == 46) && (event.getCharCode() == 46)) 
        return true;
    if ((thousandSeparator == '.') && (event.getCharCode() == 46)) 
        return false;
    else 
        if ((thousandSeparator == ',') && (event.getCharCode() == 44)) 
            return false;
    var keyCode = event.keyCode ? event.keyCode : event.charCode;
    if ((keyCode < 48 || keyCode > 58) &&
    keyCode != 8 &&
    keyCode != 9 &&
    keyCode != 32 &&
    keyCode != 37 &&
    keyCode != 39 &&
    keyCode != 40 &&
    keyCode != 41 &&
    keyCode != 43 &&
    keyCode != 44 &&
    keyCode != 45 &&
    keyCode != 46) 
        return false;
    return true;
};

// Replaces all instances of the given substring.
String.prototype.replaceAll = function(strTarget, // The substring you want to replace
 strSubString // The string you want to replace in.
){
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);
    
    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1) {
        // Relace out the current instance.
        strText = strText.replace(strTarget, strSubString)
        
        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf(strTarget);
    }
    
    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return (strText);
};

var formatNumber = function(number){
    number = number.replaceAll(thousandSeparator, '');
    if (decimalSeparator == ',') 
        number = number.replace(decimalSeparator, '.');
    return parseFloat(number);
};

function isEmpty(obj){
    if (typeof obj == 'undefined' || obj === null || obj === '') 
        return true;
    if (typeof obj == 'number' && isNaN(obj)) 
        return true;
    if (obj instanceof Date && isNaN(Number(obj))) 
        return true;
    return false;
};

var formatDate = function(value){
    if (isEmpty(value)) 
        return '';
    try {
        return value ? ((value.dateFormat(date_format_string) != '01/01/1970') ? value.dateFormat(date_format_string) : '') : '';
    } 
    catch (e) {
        console.debug(e);
    }
};

/**
 * format number by selected format
 * @author trungpm
 * @param number, decimal number, dec point, thou point
 * @return formated number
 */
function number_format_extra(number, decimals, dec_point, thousands_sep){
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // +      input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +   improved by: davook
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Jay Klehr
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Theriault
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    // *    example 10: number_format('1.20', 2);
    // *    returns 10: '1.20'
    // *    example 11: number_format('1.20', 4);
    // *    returns 11: '1.2000'
    // *    example 12: number_format('1.2000', 3);
    // *    returns 12: '1.200'
    var n = !isFinite(+number) ? 0 : +number, prec = !isFinite(+decimals) ? 0 : Math.abs(decimals), sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, dec = (typeof dec_point === 'undefined') ? '.' : dec_point, s = '', toFixedFix = function(n, prec){
        var k = Math.pow(10, prec);
        return '' + Math.round(n * k) / k;
    };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
};

/**
 * check is numeric
 * @author trungpm
 */
function isNumeric(sText, decimals, negatives){
    var isNumber = true;
    var numDecimals = 0;
    var validChars = "0123456789";
    if (decimals) 
        validChars += ".";
    if (negatives) 
        validChars += "-";
    var thisChar;
    for (i = 0; i < sText.length && isNumber == true; i++) {
        thisChar = sText.charAt(i);
        if (negatives && thisChar == "-" && i > 0) 
            isNumber = false;
        if (decimals && thisChar == ".") {
            numDecimals = numDecimals + 1;
            if (i == 0 || i == sText.length - 1) 
                isNumber = false;
            if (numDecimals > 1) 
                isNumber = false;
        }
        if (validChars.indexOf(thisChar) == -1) 
            isNumber = false;
    }
    return isNumber;
};

/**
 * check is string
 * @author trungpm
 */
function is_string(input){
    return typeof(input) == 'string';
};

/**
 * Email validitor
 * @author datnh
 */
function emailChecker(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return email.match(reg);
};

/**
 * Replace sub string for inner string
 * Sample:
 * var x = strReplaceAll('1000.000.000,00', '.', '');
 * var x = strReplaceAll(x, ',', '.');
 * alert(x);
 */
var strReplaceAll = function(strText, strReplace, subStr){
	if(typeof(strText) != 'string') return '';
	var intIndexOfMatch = strText.indexOf(strReplace);
	while (intIndexOfMatch != -1) {
		strText = strText.replace(strReplace, subStr);        
		intIndexOfMatch = strText.indexOf(strReplace);
	};
	return strText;
};

/** 
 * Format a number to money format
 * 
 * @param c
 * @param d (decimalSeparator)
 * @param t (thousandSeparator)
 * @return
 * Sample:
 * var my_num = 123654987000;
 * alert(my_num.formatMoney(2, ',', '.'));
 */
var formatMoney = function(c, d, t){
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
    + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

/**
 * Conver money to fload number
 * 
 * @param moneyStr
 * @param thousandSeparator
 * @param decimalSeparator
 * @return
 * Sample: 
 * var money = '1000.000.000,00';
 * alert(convertMoney2Fload(money, '.', ','));
 */
var convertMoney2Fload = function(moneyStr, thousandSeparator, decimalSeparator) {
	if(isEmpty(moneyStr)) return 0;
	moneyStr = strReplaceAll(moneyStr, thousandSeparator, '');
	if(decimalSeparator == ',') {
		moneyStr = moneyStr.replace(decimalSeparator, '.');
	};
	return parseFloat(moneyStr);
};


